import java.util.Stack;

public class Test {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
        stack.push(6);
        stack.push(7);
    }


    //最小栈
    class MinStack{
        private Stack<Integer> stack;
        private Stack<Integer> minstack;

        public  MinStack() {
            stack = new Stack<>();
            minstack = new Stack<>();
        }

        public void push(int val) {
            stack.push(val);
            if(minstack.empty()){
                minstack.push(val);
            }else{
                int topVal = minstack.peek();
                if(topVal >= val){
                    minstack.push(val);
                }
            }
        }

        public void pop() {
            int top = stack.pop();
            if(top == minstack.peek()){
                minstack.pop();
            }
        }

        public int top() {
            return stack.peek();
        }

        public int getMin() {
            return minstack.peek();
        }
    }


    //括号匹配
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(ch == '(' || ch == '[' || ch == '{'){
                stack.push(ch);
            }else{
                if(stack.empty()) return false;
                char top = stack.peek();
                if(top == '(' && ch == ')'
                        || top == '[' && ch == ']'
                        || top == '{' && ch == '}'){
                    stack.pop();
                }else return false;
            }
        }
        if(!stack.empty()) return false;
        return true;
    }


    //栈的压入，弹出序列
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while(j < popV.length && !stack.empty() && stack.peek() == popV[j]){
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }


    //逆波兰表达式求值
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            String tmp = tokens[i];
            if(!isOperation(tmp)){
                stack.push(Integer.parseInt(tmp));
//                stack.push(Integer.valueOf(tmp));
                //字符串转整形：Integer.parseInt / Integer.valueOf
            }else{
                Integer val2 = stack.pop();
                Integer val1 = stack.pop();
                switch (tmp){
                    case "+":
                        stack.push(val1 + val2);
                        break;
                    case "-":
                        stack.push(val1 - val2);
                        break;
                    case "*":
                        stack.push(val1 * val2);
                        break;
                    case "/":
                        stack.push(val1 / val2);
                        break;
                }
            }
        }
        return stack.pop();
    }

    private boolean isOperation(String tmp){
        if(tmp.equals("+") || tmp.equals("-") || tmp.equals("*") || tmp.equals("/")){
            return true;
        }else{
            return false;
        }
    }

}
